package com.mybatis.lo.model;

import com.mybatis.lo.utils.GenericTokenParser;

import lombok.AllArgsConstructor;

/**
 * 封装了`${}`和动态标签的SQL信息
 * @author zhangbh
 * 2023-06-09 20:08
 **/
@AllArgsConstructor
public class DynamicSqlSource implements SqlSource {

    /**
     * 复合查询节点
     */
    private SqlNode mixedSqlNode;

    @Override
    public BoundSql getBoundSql(Object param) {
        DynamicContext context = new DynamicContext(param);
        ParameterMappingTokenHandler tokenHandler = new ParameterMappingTokenHandler();
        GenericTokenParser parser = new GenericTokenParser("#{", "}", tokenHandler);
        mixedSqlNode.apply(context);
        // 处理sql
        String parse = parser.parse(context.getSql());
        return new BoundSql(parse, tokenHandler.getParameterMappings());
    }
}
